package com.daocao.hua.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.CircleImageView;
import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.daocao.hua.R;
import com.daocao.hua.api.Api;
import com.daocao.hua.constant.UserGlobal;
import com.daocao.hua.fragment.CommissionRankingFragment;
import com.daocao.hua.fragment.CommissionRankingSecFragment;
import com.daocao.hua.model.ShareRankingModel;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.PreferenceUtils;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.Spans;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class CommissionRankingActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_icon)
    CircleImageView imgIcon;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_commission_today)
    TextView txtCommissionToday;
    @BindView(R.id.txt_commission_total)
    TextView txtCommissionTotal;
    @BindView(R.id.txt_commission_month)
    TextView txtCommissionMonth;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    @BindView(R.id.txt_rank_top)
    TextView txtRankTop;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commission_order);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("佣金排行榜");
        txtName.setText(UserGlobal.getUserName());
        if (PreferenceUtils.getInt(this, "sex", 0) == 0) {
            Glide.with(this).load(PreferenceUtils.getString(this, "header", "")).dontAnimate().placeholder(R.mipmap.img_my_girl).error(R.mipmap.img_my_girl).into(imgIcon);
        } else {
            Glide.with(this).load(PreferenceUtils.getString(this, "header", "")).dontAnimate().placeholder(R.mipmap.img_my_boy).error(R.mipmap.img_my_boy).into(imgIcon);
        }
        getShareRanking();
    }

    private void setTopData(String head, String bottom, TextView textView) {
        if (TextUtils.isEmpty(bottom)) {
            bottom = "";
        }
        Spanned span = Spans.with(this).font(head, getResources().getDimensionPixelSize(R.dimen.x24), 0xffFFFFFF).
                font("\n" + bottom, getResources().getDimensionPixelSize(R.dimen.x44), 0xffFFFFFF).
                build();
        textView.setText(span);
    }

    private void setData(String head, String bottom, TextView textView) {
        if (TextUtils.isEmpty(bottom)) {
            bottom = "";
        }
        Spanned span = Spans.with(this).font(head, getResources().getDimensionPixelSize(R.dimen.x24), 0xffF9FAFB).
                font("\n" + bottom, getResources().getDimensionPixelSize(R.dimen.x34), 0xffF9FAFB).
                build();
        textView.setText(span);
    }

    public void getShareRanking() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }

        loadingDialog.show();
        Api.api_service.getShareRanking().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ShareRankingModel.DataBean.UserBean userdata = result.data.user;
                    setData("今日佣金(元)", userdata.today_reward, txtCommissionToday);
                    setData("总佣金(元)", userdata.total_reward, txtCommissionTotal);
                    setData("本月佣金(元)", userdata.month_reward, txtCommissionMonth);

                    if (userdata.today_rank.equals("0")) {
                        txtRankTop.setText("未进\n榜单");
                    } else {
                        setTopData("排行榜", userdata.today_rank, txtRankTop);
                    }


                    String[] titles = new String[]{"今日榜单", "历史总榜"};
                    CommissionRankingFragment fragment1 = new CommissionRankingFragment();
                    fragment1.setData(result.data.today_list);
                    CommissionRankingSecFragment fragment2 = new CommissionRankingSecFragment();
                    fragment2.setData(result.data.total_list);
                    Fragment[] fragments = {fragment1, fragment2};


                    pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
                        @Override
                        public CharSequence getPageTitle(int position) {
                            return titles[position];
                        }

                        @Override
                        public Fragment getItem(int position) {
                            return fragments[position];
                        }

                        @Override
                        public int getCount() {
                            return fragments.length;
                        }
                    });
                    tabLayout.setupWithViewPager(pager);
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                Util.startActivity(this, WithdrawV2Activity.class);
                break;
        }
    }
}
